package com.frame.demo.exception;

import com.frame.demo.rest.ExceptionDispose;
import com.frame.demo.rest.RestError;

/**
 * @author yuefeng
 * @version 3.4.1
 * @date 16/1/25
 */
public class ExceptionUtil {
    public static RestError gennerateRestError(Throwable pThrowable) {
        if (null == pThrowable) return new RestError("未知异常错误!");
        String _msgStr = pThrowable.getMessage();
        int _code = 0;
        int _type;

        if (pThrowable instanceof XKTickOutException) {
            _type = ExceptionDispose.EXCEPTION_TICK_OUT;
        } else if (pThrowable instanceof ValidityException) {
            _type = ExceptionDispose.EXCEPTION_VALIDITY;
        } else if (pThrowable instanceof NoNetworkException
                || pThrowable instanceof NetworkTimeOutException
                || pThrowable instanceof NetworkErrorExceptin
                || pThrowable instanceof ServerBusyExceptin) {
            _type = ExceptionDispose.EXCEPTION_NETWORK;
        } else if (pThrowable instanceof DirtyDataException) {
            _type = ExceptionDispose.EXCEPTION_DIRTY_DATA;
        } else if (pThrowable instanceof DDYCBizException) {
            _type = ExceptionDispose.SUCCESS;
        } else {
            _type = ExceptionDispose.EXCEPTION_BUSINESS;
            _msgStr = "数据异常";
        }

        if (pThrowable instanceof DDYCBizException) {
            DDYCBizException _exception = (DDYCBizException) pThrowable;
            _code = _exception.getCode();
        }


        RestError _restError = new RestError(_msgStr);
        _restError.setCode(_code);
        _restError.setErrorType(_type);
        return _restError;
    }
}
